#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mg_timer.h"
#include "mg_Rs485Protocol.h"
#include "mg_Device.h"
#include "mg_heating.h"
#include "mg_Sys_Led.h"
#include "mg_Gesture.h"

volatile STRUCT_TIME mgSysTime;

uint16_t volatile Timer10ms = 0;
uint16_t volatile Timer100ms = 0;
uint16_t volatile Timer1s = 0;
uint16_t volatile Timer3s = 0;
uint16_t volatile Timer10s = 0;
uint16_t volatile Timer15s = 0;
uint16_t volatile Timer30s = 0;
uint16_t volatile Timer60s = 0;
uint16_t volatile Timer4h = 0;
//定时器初始化
void timer_init(void)
{
	ITHIntrHandler timer_100us, Timer_s;
	timer_100us = timer_100us_isr;
	Timer_s = timer_s_isr;

	ithTimerReset(MS_TIMER);
	//ithTimerIsrEnable(MS_TIMER);
	//初始化定时器中断（Initialize Timer IRQ）
	ithIntrDisableIrq(MS_TIMER_INIR);
	ithIntrClearIrq(MS_TIMER_INIR);
	//注册定时器程序到IRQ（register Timer Handler to IRQ）
	ithIntrRegisterHandlerIrq(MS_TIMER_INIR, timer_100us_isr, (void*)MS_TIMER);
	//将定时器IRQ设置为边缘触发（set Timer IRQ to edge trigger）
	ithIntrSetTriggerModeIrq(MS_TIMER_INIR, ITH_INTR_EDGE);
	//设置定时器IRQ检测上升沿（set Timer IRQ to detect rising edge）
	ithIntrSetTriggerLevelIrq(MS_TIMER_INIR, ITH_INTR_HIGH_RISING);
	//开启定时器中断（Enable Timer IRQ）
	ithIntrEnableIrq(MS_TIMER_INIR);
	//设置定时器中断时间1000us(1ms)
	ithTimerSetTimeout(MS_TIMER, 100);
	//使能定时器
	ithTimerCtrlEnable(MS_TIMER, ITH_TIMER_EN);
	//重启定时器
	ithTimerReset(S_TIMER);
	//ithTimerIsrEnable(MS_TIMER);
	//初始化定时器中断（Initialize Timer IRQ）
	ithIntrDisableIrq(S_TIMER_INIR);
	ithIntrClearIrq(S_TIMER_INIR);
	//注册定时器程序到IRQ（register Timer Handler to IRQ）
	ithIntrRegisterHandlerIrq(S_TIMER_INIR, Timer_s, (void*)S_TIMER);
	//将定时器IRQ设置为边缘触发（set Timer IRQ to edge trigger）
	ithIntrSetTriggerModeIrq(S_TIMER_INIR, ITH_INTR_EDGE);
	//设置定时器IRQ检测上升沿（set Timer IRQ to detect rising edge）
	ithIntrSetTriggerLevelIrq(S_TIMER_INIR, ITH_INTR_HIGH_RISING);
	//开启定时器中断（Enable Timer IRQ）
	ithIntrEnableIrq(S_TIMER_INIR);
	//设置定时器中断时间1000us(1ms)
	ithTimerSetTimeout(S_TIMER, 1000);
	//使能定时器
	ithTimerCtrlEnable(S_TIMER, ITH_TIMER_EN);
}
//1ms定时器中断
uint32_t volatile Timems = 0;
uint32_t volatile counts = 0;
void timer_100us_isr(void* arg)
{
	ithTimerSetTimeout(MS_TIMER, 100);	
	Timems++;
	if (counts == 0)
	{
		Bee_Check_Cmd();
		if (report_Bee_Cmd_dbg() == 1)
		{
			counts = 1000;
		}
		else
			counts = 12;
	}
	else
		counts--;
}
//1s定时器中断
void timer_s_isr(void* arg)
{
	ithTimerSetTimeout(S_TIMER, 1000);//设置定时器中断时间1000000us(1s)
	Gesture_Timer_Poll();
	if (Timer10ms++ == (10 - 1))//10ms定时
	{
		Timer10ms = 0;
		RS485ReSendProcessTime(); //重发计数器
	}
	if (Timer1s++ == (1000 - 1))//1s定时
	{
		Timer1s = 0;
		//Sys_Led_Turn();//系统运行灯
		ResetTimeOverCheck();//恢复出厂设置模式超时检测
		Set_State_MessageFlag_1s();//设置获取设备状态的消息标志
		Screensaver_Timer();//屏保滚动计时
		if (Timer10s++ == (10 - 1))//10s定时
		{
			if (!MessageFlag[MSG_HAVE_GET_TIME])//还没有获取主机时间
				MessageFlag[MSG_GET_TIME] = ON;//与主机进行对时
			Timer10s = 0;
		}
		if (Timer60s++ == (60 - 1))//1分钟定时
		{
			Timer60s = 0;
			Heat_Timer_Handle();
			if (MessageFlag[MSG_SET_ADDR_TIME_OVER] == ON)
			{
				MessageFlag[MSG_SET_ADDR_TIME_OVER] = OFF;
			}
			if (Timer4h++ == (240 - 1))
			{
				Timer4h = 0;
				MessageFlag[MSG_GET_TIME] = ON;//与主机进行对时
			}
		}
	}
}

//时间初始化
void RealTimeInit(void)
{
	mgSysTime.year	= 20+101;
	mgSysTime.mon	= 5;
	mgSysTime.day	= 4+1;
	mgSysTime.hour	= 13;
	mgSysTime.min	= 15;
	mgSysTime.sec	= 0;
	Set_Sys_Time(mgSysTime);

}
//设置系统时间时钟时间
void Set_Sys_Time(STRUCT_TIME RealTime)
{
	struct timeval tv;
	struct tm *tm, mytime;

	gettimeofday(&tv, NULL);
	tm = localtime(&tv.tv_sec);

	memcpy(&mytime, tm, sizeof (struct tm));

	mytime.tm_year = RealTime.year;
	mytime.tm_mon = RealTime.mon;
	mytime.tm_mday = RealTime.day;
	mytime.tm_hour = RealTime.hour;
	mytime.tm_min = RealTime.min;
	mytime.tm_sec = RealTime.sec;

	tv.tv_sec = mktime(&mytime);
	tv.tv_usec = 0;

	settimeofday(&tv, NULL);
	return;
}
struct tm Get_Sys_Time(void)
{
	struct timeval tv;
	struct tm *tm, mytime;

	gettimeofday(&tv, NULL);
	tm = localtime(&tv.tv_sec);


	mytime.tm_year = tm->tm_year+1900;
	mytime.tm_mon = tm->tm_mon+1;
	mytime.tm_mday = tm->tm_mday;
	mytime.tm_hour = tm->tm_hour;
	mytime.tm_min = tm->tm_min;
	mytime.tm_sec = tm->tm_sec;

	return mytime;
}